package model;

import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：徐志荣
 * @Date：2023/5/27 下午 3:09
 *
 * 针对博客表进行的基本操作
 */
public class BlogDao {
    //1. 新增一个博客
    public void add(Blog blog){
        //1. 和数据库建立链接
        Connection connection = null;
        PreparedStatement statement = null;
        try {
             connection = DBUtil.getConnection();
            String sql = "insert into blog values(null,?,?,now(),?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            //statement.setTimestamp(3,blog.getPostTimestamp());
            statement.setInt(3,blog.getUserId());
            //3.执行sql
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }


    }
    //2.根据博客id来查询指定博客(博客详情页中)
    public Blog selectById(int blogId){
        //1. 和数据库建立链接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            //3.执行sql
            resultSet =  statement.executeQuery();

            //4. 遍历结果集合,因为blogId是主键,有且仅有一条数据
            if(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    //3.直接查询出数据库中所有的博客列表(用于博客列表页)
    public List<Blog> selectAll(){
        //1. 和数据库建立链接
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> blogList = new ArrayList<>();
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            //3.执行sql
            resultSet =  statement.executeQuery();
            //4. 遍历结果集合,因为blogId是主键,有且仅有一条数据

            while(resultSet.next()){

               Blog blog = new Blog();
               blog.setBlogId(resultSet.getInt("blogId"));
               blog.setTitle(resultSet.getString("title"));
               String content =(resultSet.getString("content"));
               if(content.length() >= 100){
                   content = content.substring(0,100)+ "...";
               }
                blog.setContent(content);
               blog.setPostTime(resultSet.getTimestamp("postTime"));
               blogList.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return blogList;
    }
    //4. 删除指定博客
    public void delete (int blogId){
        //1. 和数据库建立链接
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete  from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            //3.
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }

}
